package com.gitee.kamismile.disruptorEx.commandhandling.disruptor;

import com.lmax.disruptor.EventHandler;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

public class CommandHandlerInvoker implements EventHandler<CommandHandlingEntry> {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final ThreadLocal<CommandHandlerInvoker> CURRENT_INVOKER = new ThreadLocal<>();

    @Override
    public void onEvent(CommandHandlingEntry commandHandlingEntry, long l, boolean b) throws Exception {
        commandHandlingEntry.setResult(commandHandlingEntry.getGenericMessage().getPayload());
        commandHandlingEntry.getCallback()
                .onSuccess(commandHandlingEntry.getGenericMessage().getCommandMessage(),
                commandHandlingEntry.getResult());
    }

    @Override
    public void onStart() {
        CURRENT_INVOKER.set(this);
    }

    @Override
    public void onShutdown() {
        CURRENT_INVOKER.remove();
    }
}
